/**
 * Created with JetBrains WebStorm.
 * User: Administrator
 * Date: 13-11-29
 * Time: 下午5:44
 * To change this template use File | Settings | File Templates.
 */
Ext.define("byb.controller.BaseController",{
    extend:"Ext.app.Controller",
    views:[],
    models: [],
    stores:[],
    goToView:null,//当前所要展现的页面
    init:function(){
        this.control({
            "AccordionView button":{
                LeftMenuSelectedButton:this.functionItemClick
            },
            "combo":{
                change:this.comboReset,
                afterrender:this.comboAfterRender
            },
            "combo[name=sheetTypeId]":{
                afterrender:function(ctx){
                    ctx.store.setCompoDefaultValue("sheetTypeId");
                }
            }
        });
        this.callParent(arguments);
    },
    functionItemClick:function(buttonObject){
        this.unloadData();
        this.goToView=buttonObject.object.resource;
    },
    queryWidgetByName:function(name){
       var grid= Ext.ComponentQuery.query(name)[0];
       return grid;
    },
    q: function(name){
        return this.queryWidgetByName(name);
    },
    comboReset:function(ctx, e,eOpts){
        if(ctx.rawValue===""){
            ctx.reset();
        }
    },
    comboAfterRender: function(ctx,eOpts){
    	ctx.minChars=1;
    },
    /**
     * 对当前的store进行添加和删除后重新加载
     * @param store
     * @param isAdd
     */
    reload:function(store){
        var count=store.getCount();
        if(count===0&&store.currentPage>1){
            store.previousPage();
        }else{
            store.reload();
        }
    },
    /**
     * 对基本的model校验
     * @param model
     * @return
     */
    modelValidate:function(model){
        var errors=model.validate();
        var errorMsg=[];
        var isValid=errors.isValid();
        if (!isValid) {
            errors.each(function(error) {
                errorMsg.push(error.message);
            });
            Ext.Msg.alert("操作提示", errorMsg.join("<br/>"));
        }
        return isValid;
    },
    /**
     * 对工单模块的校验
     * @param model
     * @param sheetStatusCode
     */
    sheetModelValidate:function(model,sheetStatusCode){
        if(model.data.hasOwnProperty("sheetType")){
            byb.Constant.SheetCurrentType=model.data.sheetType;
        }
        if(sheetStatusCode==="SOT_OW_NORMAL"||sheetStatusCode==="SOT_IW_NORMAL"){
           return this.modelValidate(model);
        }else{
           return true;
        }
    },
    /**
     * 报表页面 设置高度
     */
    setReportPanelHeight : function(panel){
    	var fatherPanel = panel;
    	var reportPanel = Ext.ComponentQuery.query('reportPanel')[0];
    	reportPanel.setHeight(fatherPanel.getHeight() - 85);
    	fatherPanel.on('resize', function(ctx, width, height, oldWidth, oldHeight, eOpts){
    		reportPanel.setHeight(ctx.getHeight() - 85);
    	})
    },
    /**
     * 查询表格的头信息
     * 主要用于条目的查询
     */
    queryGridHeaderInfo:function(model,callback){
        var view=this.queryWidgetByName(this.goToView);
        var form=view.down("form");
        var values= form.getValues();
        Ext.Ajax.request({
            url:byb.Constant.APIRoot+"/"+model+"/querySumInfoOfSheetHeader",
            params: {form:Ext.encode(values)},
            success: function(response){
                var text=response.responseText;
                var obj=Ext.decode(text);
                if(obj){
                     callback.call(this,obj);
                }
            }
        });
    },
    unloadData:function(){

    }
});